package org.frog.sgs.client.game;

import java.io.Serializable;
import java.util.Map;
import java.util.TreeMap;

import org.apache.mina.core.session.IoSession;
import org.frog.sgs.card.basic.Basic;
import org.frog.sgs.card.equipment.Equipment;
import org.frog.sgs.card.general.General;
import org.frog.sgs.card.role.Role;
import org.frog.sgs.card.scheme.type.JudgeType;
import org.frog.sgs.card.strength.Strength;
import org.frog.sgs.character.Character;
import org.frog.sgs.io.SgsMsg;

/**
 * @author duanxiangqi
 * @date 2012-10-26
 */
public class ClientInstance implements Serializable {

	private static final long serialVersionUID = 1299061611022856220L;

	private IoSession session;

	private Role role;

	private int position;

	private General general;

	private Strength strength;

	// 手牌
	private Map<Integer, Basic> cardsInHand = new TreeMap<Integer, Basic>();

	// 装备区
	private Map<Integer, Equipment> equipments = new TreeMap<Integer, Equipment>();

	// 判定区
	private Map<JudgeType, Basic> judgeSchemes = new TreeMap<JudgeType, Basic>();

	// 所有玩家
	private Map<Integer, Character> characters = new TreeMap<Integer, Character>();

	public ClientInstance(IoSession session) {
		this.session = session;
	}

	public void showSelf() {
		System.out.println("您的身份是：\t" + role.getViewName() + " " + general.getViewName());
		System.out.println("您当前的体力是：\t" + strength.getViewName());
		System.out.println("您当前的手牌是：\t");
		for (Basic card : cardsInHand.values()) {
			System.out.println("\t" + card.getViewName());
		}
		System.out.println("您当前的装备是：");
		for (Equipment card : equipments.values()) {
			System.out.println("\t" + card.getViewName());
		}
		System.out.println("您当前的判定牌是：");
		for (JudgeType type : judgeSchemes.keySet()) {
			System.out.println("\t" + type.getName() + "：" + judgeSchemes.get(type).getViewName());
		}
	}

	public void showAll() {
		System.out.println("当前所有玩家状态：");
		for (Character character : characters.values()) {
			System.out.print("\t"
					+ (character.getPosition() != position ? character.getViewName() : ("[自己" + position + ":"
							+ role.getViewName() + "]")) + "，" + character.getGeneral().getViewName() + "，体力 "
					+ character.getStrength().getStrength() + "/" + character.getStrength().getMaxStrength() + "，"
					+ (character.isDie() ? "死亡" : "") + "，手牌" + character.getCardsInHand().size() + "张，装备{");
			for (Equipment equipment : character.getEquipments().values()) {
				System.out.print(equipment.getPosition() + "：" + equipment.getViewName());
			}
			System.out.print("}，判定牌{");
			for (JudgeType judgeType : character.getJudgeSchemes().keySet()) {
				System.out.print(judgeType.getName() + "：" + character.getJudgeSchemes().get(judgeType).getViewName());
			}
			System.out.println("}");
		}
	}

	public String getName() {
		return "[character" + position + ":" + (role == null ? "" : (role.getViewName())) + "]";
	}

	public void sendMsg(SgsMsg msg) {
		session.write(msg);
	}

	public IoSession getSession() {
		return session;
	}

	public void setSession(IoSession session) {
		this.session = session;
	}

	public Role getRole() {
		return role;
	}

	public void setRole(Role role) {
		this.role = role;
	}

	public General getGeneral() {
		return general;
	}

	public void setGeneral(General general) {
		this.general = general;
	}

	public Strength getStrength() {
		return strength;
	}

	public void setStrength(Strength strength) {
		this.strength = strength;
	}

	public int getPosition() {
		return position;
	}

	public void setPosition(int position) {
		this.position = position;
	}

	public Map<Integer, Basic> getCardsInHand() {
		return cardsInHand;
	}

	public void setCardsInHand(Map<Integer, Basic> cardsInHand) {
		this.cardsInHand = cardsInHand;
	}

	public Map<Integer, Equipment> getEquipments() {
		return equipments;
	}

	public void setEquipments(Map<Integer, Equipment> equipments) {
		this.equipments = equipments;
	}

	public Map<JudgeType, Basic> getJudgeSchemes() {
		return judgeSchemes;
	}

	public void setJudgeSchemes(Map<JudgeType, Basic> judgeSchemes) {
		this.judgeSchemes = judgeSchemes;
	}

	public Map<Integer, Character> getCharacters() {
		return characters;
	}

	public void setCharacters(Map<Integer, Character> characters) {
		this.characters = characters;
	}
}
